package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.CartDao;
import org.csu.mypetstore.persistence.CategoryDao;
import org.csu.mypetstore.persistence.ItemDao;
import org.csu.mypetstore.persistence.ProductDao;
import org.csu.mypetstore.persistence.impl.CartDaoImpl;
import org.csu.mypetstore.persistence.impl.CategoryDaoImpl;
import org.csu.mypetstore.persistence.impl.ItemDaoImpl;
import org.csu.mypetstore.persistence.impl.ProductDaoImpl;

import java.util.List;

public class CatalogService {
    private CategoryDao categoryDao;
    private ProductDao productDao;
    private ItemDao itemDao;

    public CatalogService()
    {
        categoryDao = new CategoryDaoImpl();
        productDao =  new ProductDaoImpl();
        itemDao = new ItemDaoImpl();
    }

    public List<Category>getCategoryList()
    {
        return  categoryDao.getCategoryList();
    }

    public Category getCategory(String categoryId)
    {
        return categoryDao.getCategory(categoryId);
    }

    public List<Product>getProductListByCategory(String categoryId)
    {
        return productDao.getProductListByCategory(categoryId);
    }

    public List<Product>searchProductList(String keywords)
    {
//        System.out.println(keywords);
//        System.out.println( productDao.searchProductList("%"+keywords+"%").size());
        return productDao.searchProductList("%"+keywords+"%");
    }

    public List<Product>searchProductListWithLimit(String keywords)
    {
//        System.out.println(keywords);
//        System.out.println( productDao.searchProductList("%"+keywords+"%").size());
        return productDao.searchProductListWithLimit("%"+keywords+"%");
    }

    public Product getProduct(String productId)
    {
        return productDao.getProduct(productId);
    }

    public List<Item>getItemListByProduct(String productId)
    {
        return  itemDao.getItemListByProduct(productId);
    }

    public Item getItem(String itemId)
    {
        return  itemDao.getItem(itemId);
    }

    public boolean isItemInStock(String itemId)
    {
        return itemDao.getInventoryQuantity(itemId)>0;
    }

    public static Cart addCart(Account account, Cart cart)
    {
        CartDao cartDao = new CartDaoImpl();
        List<String>items = cartDao.getItems(account);
   //     System.out.println("----------------"+account.getUsername()+"-----------------------------");
        for(int i=0;i<items.size();i++)
        {
            String workingItemId = items.get(i);
            if(cart.containsItemId(workingItemId)){
                cart.incrementQuantityByItemId(workingItemId);
            }else{
                CatalogService service = new CatalogService();
                boolean isInStock = service.isItemInStock(workingItemId);
                Item item = service.getItem(workingItemId);
                cart.addItem(item,isInStock);
            }
        }

        return  cart;
    }

    public static void deleteCart(Account account,String itemId)
    {
        CartDao cartDao = new CartDaoImpl();
        cartDao.deleteCart(account,itemId);
    }

    public static void insertCart(Account account,String itemId)
    {
        CartDao cartDao = new CartDaoImpl();
        cartDao.insertCart(account,itemId);
    }

    public static void clearUpCart(Account account)
    {
        CartDao cartDao = new CartDaoImpl();
        cartDao.clearUpCart(account);
    }

    public static void minusCart(Account account,String itemId)
    {
        CartDao cartDao = new CartDaoImpl();
        cartDao.minusCart(account,itemId);
    }



}
